﻿//     Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace Gateway.Console.Models
{
    public class FlightModel
    {
        static readonly char[] ListDelimiter = new char[] { ';' };

        [Key]
        public int Id { get; set; }

        [Required]
        [Column("RoleId")]
        public int RoleId { get; set; }
        [ForeignKey("RoleId")]
        public virtual RoleModel Role { get; set; }

        [Display(Name = "Distribution Weight")]
        [Required]
        [Range(1.0, 100000000000.0)]
        public int Weight { get; set; }

        [Display(Name = "Target Address")]
        [Required]
        public string Redirect { get; set; }

        [Display(Name = "IP Addresses")]
        public string Addresses { get; set; }

        [Display(Name = "Users")]
        public string Users { get; set; }

        [Display(Name = "Tenants")]
        public string Tenants { get; set; }

        [NotMapped]
        public bool IsNonWeightedFlight
        {
            get { return this.AddressList.Any() || this.UserList.Any() || this.TenantList.Any(); }
        }
        
        [NotMapped]
        public IEnumerable<string> AddressList
        {
            get { return String.IsNullOrWhiteSpace(this.Addresses) ? new string[0] : this.Addresses.Split(ListDelimiter, StringSplitOptions.RemoveEmptyEntries); }
        }

        [NotMapped]
        public IEnumerable<string> UserList
        {
            get { return String.IsNullOrWhiteSpace(this.Users) ? new string[0] : this.Users.Split(ListDelimiter, StringSplitOptions.RemoveEmptyEntries); }
        }

        [NotMapped]
        public IEnumerable<string> TenantList
        {
            get { return String.IsNullOrWhiteSpace(this.Tenants) ? new string[0] : this.Tenants.Split(ListDelimiter, StringSplitOptions.RemoveEmptyEntries); }
        }
    }
}